package cn.wdm.ymcc.service.impl;

import cn.wdm.ymcc.domain.Block;
import cn.wdm.ymcc.domain.MessageSms;
import cn.wdm.ymcc.mapper.BlockMapper;
import cn.wdm.ymcc.mapper.MessageSmsMapper;
import cn.wdm.ymcc.service.IBlockService;
import cn.wdm.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lao.zhang
 * @since 2022-07-20
 */
@Service
public class BlockServiceImpl extends ServiceImpl<BlockMapper, Block> implements IBlockService {
    @Autowired
    private BlockMapper blockMapper;
    @Autowired
    private MessageSmsMapper messageSmsMapper;

    /**
     * 被拉黑 保存拉黑信息，设置短信库中的状态为拉黑
     * @param block
     */
    @Override
    public void block(Block block) {
        AssertUtil.isNotNull(block,"拉黑对象为空");
        block.setBlacktime(new Date());
        //修改状态为拉黑
        MessageSms messageSms = messageSmsMapper.selectById(block.getMessageId());
        //拉黑
        messageSms.setBlackorwhite(0);
        messageSmsMapper.updateAllColumnById(messageSms);
        block.setMessageId(block.getMessageId());
        blockMapper.insert(block);
        //blockMapper.saveBlock(block);
    }
    /**
     * 移除黑名单
     * @param block
     */
    @Override
    public void outBlockList(Block block) {
        String ip = block.getIp();
        String phone = block.getPhone();
        AssertUtil.isNotNull(ip,"Ip为空");
        AssertUtil.isNotNull(phone,"电话为空");
        //1.删除t_block中的数据
        Wrapper<Block> tWrapper = new EntityWrapper();
        tWrapper.eq("phone",phone);
        blockMapper.delete(tWrapper);
        //2.修改messageSms中的状态
        MessageSms messageSms = messageSmsMapper.selectById(block.getMessageId());
        messageSms.setBlackorwhite(1);
        messageSmsMapper.updateById(messageSms);
    }
   /* @Override
    public Block selectByIp(String ip) {
        return blockMapper.selectByIp(ip);
    }*/

   /* @Override
    public Block selectByPhone(String phone) {
        return blockMapper.selectByPhone(phone);
    }*/
}
